//
//  DBAlertView.swift
//  DBCA
//
//  Created by mugua on 2024/7/6.
//

import Foundation
enum alertViewType: String {
    case apply = "alert_apply_logo"
    case identity = "alert_identity"
    case loginOut = "alert_login_out"
    case deleteAccount = "alert_delete_account"
    case normal
}

typealias ConfirmBlock = (()->Void)
typealias CancelBlock = (()->Void)

class DBAlertView: UIView {
    
    let disposeBag = DisposeBag()
    
    class func showAlertView(type:alertViewType, title: String, message: String, confirmBtnText: String, cancelBtnText: String, confirmBlock: ConfirmBlock, cancelBlock: CancelBlock) {
        let alertView = DBAlertView()
        alertView.initUI()
        alertView.initData(type: type, title: title, message: message, confirmBtnText: confirmBtnText, cancelBtnText: cancelBtnText, confirmBlock: confirmBlock, cancelBlock: cancelBlock)
        alertView.show()
    }
    
    private func initUI(){
        DBKeyWindow?.addSubview(self)
        self.addSubview(bgView)
        bgView.addSubview(contentView)
        contentView.addSubview(bgImgView)
        contentView.addSubview(closeBtn)
        contentView.addSubview(logoImgView)
        contentView.addSubview(titleLabel)
        contentView.addSubview(messageLabel)
        contentView.addSubview(topBtn)
        contentView.addSubview(bottomBtn)
        contentView.addSubview(agreeBtn)
        contentView.addSubview(agreeLabel)
        contentView.addSubview(hLineView)
        contentView.addSubview(vLineView)
        
        self.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        bgView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        contentView.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.width.equalTo(DBScreenWidth-90)
        }
        
        bgImgView.snp.makeConstraints { make in
            make.left.top.right.equalToSuperview()
            make.height.equalTo(164)
        }
        
        closeBtn.snp.makeConstraints { make in
            make.right.equalTo(-13)
            make.top.equalTo(12)
            make.width.height.equalTo(22)
        }
        
        logoImgView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.bottom.equalTo(contentView.snp.top).offset(66)
        }
        
        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(18)
            make.right.equalTo(-18)
            make.top.equalTo(86)
        }
        
        messageLabel.snp.makeConstraints { make in
            make.left.equalTo(18)
            make.right.equalTo(-18)
            make.top.equalTo(titleLabel.snp.bottom).offset(10)
        }
        
        topBtn.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.left.equalTo(28)
            make.right.equalTo(-28)
            make.height.equalTo(40)
            make.top.equalTo(messageLabel.snp.bottom).offset(15)
        }
        
        bottomBtn.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.left.equalTo(28)
            make.right.equalTo(-28)
            make.height.equalTo(40)
            make.top.equalTo(topBtn.snp.bottom).offset(10)
        }
        
        agreeBtn.snp.makeConstraints { make in
            make.left.equalTo(28)
            make.top.equalTo(bottomBtn.snp.bottom).offset(10)
            make.width.height.equalTo(20)
        }
        
        agreeLabel.snp.makeConstraints { make in
            make.left.equalTo(agreeBtn.snp.right).offset(2)
            make.right.equalTo(-25)
            make.top.equalTo(bottomBtn.snp.bottom).offset(12)
            make.bottom.equalTo(-20)
        }
        
        hLineView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(messageLabel.snp.bottom).offset(19)
            make.height.equalTo(1)
        }
        
        vLineView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(messageLabel.snp.bottom).offset(19)
            make.width.equalTo(1)
            make.height.equalTo(45)
        }
    }
    
   private func initData(type:alertViewType, title: String, message: String, confirmBtnText: String, cancelBtnText: String, confirmBlock: ConfirmBlock, cancelBlock: CancelBlock) {
       
       logoImgView.image = UIImage(named: type.rawValue)
       if type != .deleteAccount {
           agreeBtn.isHidden = true
           agreeLabel.isHidden = true
           agreeLabel.snp.remakeConstraints { make in
               make.left.equalTo(agreeBtn.snp.right).offset(2)
               make.right.equalTo(-25)
               make.top.equalTo(bottomBtn.snp.bottom)
               make.height.equalTo(0)
               make.bottom.equalTo(-20)
           }
       }
       
       if type == .normal {
           hLineView.isHidden = false
           vLineView.isHidden = false
           closeBtn.isHidden = true
           
           topBtn.backgroundColor = .clear
           bottomBtn.backgroundColor = .clear
           topBtn.setTitleColor(DBViewBgColor, for: .normal)
           bottomBtn.setTitleColor(DBHexColor("#B1BEB5"), for: .normal)
           topBtn.titleLabel?.font = DBFuturaMaxiStd_DemiFont(14)
           bottomBtn.titleLabel?.font = DBFuturaMaxiStd_DemiFont(14)
           
           
           titleLabel.snp.updateConstraints { make in
               make.top.equalTo(19)
           }
           
           bottomBtn.snp.remakeConstraints { make in
               make.left.equalToSuperview()
               make.right.equalTo(vLineView)
               make.top.equalTo(hLineView)
               make.height.equalTo(45)
               make.bottom.equalToSuperview()
           }
           
           topBtn.snp.remakeConstraints { make in
               make.right.equalToSuperview()
               make.left.equalTo(vLineView)
               make.top.equalTo(hLineView)
               make.height.equalTo(45)
           }
           
           
           
           
       } else {
           
           
           
           
           
       }
       
       let titleAtt = NSMutableAttributedString(string: title)
       let paragraphStyle = NSMutableParagraphStyle()
       paragraphStyle.lineSpacing = 5
       paragraphStyle.alignment = .center
       titleAtt.addAttribute(.paragraphStyle, value: paragraphStyle, range: title.rangeOfAll())
       titleLabel.attributedText = titleAtt
       
       let messageAtt = NSMutableAttributedString(string: message)
       messageAtt.addAttribute(.paragraphStyle, value: paragraphStyle, range: message.rangeOfAll())
       messageLabel.attributedText = messageAtt
       topBtn.setTitle(confirmBtnText, for: .normal)
       bottomBtn.setTitle(cancelBtnText, for: .normal)
       
       closeBtn.rx.tap.subscribe(onNext: { [weak self] in
           guard let self = self else { return }
           self.dismiss()
       }).disposed(by: disposeBag)
       
       topBtn.rx.tap.subscribe(onNext: { [weak self] in
           guard let self = self else { return }
           
           if !self.agreeBtn.isSelected {
               DBToast.show(text: "请勾选协议")
           } else {
               self.dismiss()
           }
           
//           self.dismiss()
       }).disposed(by: disposeBag)
       
       bottomBtn.rx.tap.subscribe(onNext: { [weak self] in
           guard let self = self else { return }
           self.dismiss()
       }).disposed(by: disposeBag)
       
       agreeBtn.rx.tap.subscribe(onNext: { [weak self] in
           guard let self = self else { return }
           self.agreeBtn.isSelected.toggle()
       }).disposed(by: disposeBag)
       
       
    }
    
    private func show(){
        contentView.transform = CGAffineTransform(scaleX: CGFloat.leastNormalMagnitude, y: CGFloat.leastNormalMagnitude)
        UIView.animate(withDuration: 0.3, delay: 0.1, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, animations: {
            self.contentView.transform = CGAffineTransform(scaleX: 1, y: 1)
        })
    }
    
    private func bindEvent(){
        
    }
    
    private func closeBtnClick(){
        self.dismiss()
    }
    
    private func dismiss() {
        self.removeFromSuperview()
    }
    
    private lazy var bgView: UIView = {
        let bgView = UIView()
        bgView.backgroundColor = .black.withAlphaComponent(0.7)
        return bgView
    }()
    
    private lazy var contentView: UIView = {
        let contentView = UIView()
        contentView.backgroundColor = .white
        contentView.layer.cornerRadius = 4
        return contentView
    }()
    
    private lazy var bgImgView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "alert_bg")
        imageView.isUserInteractionEnabled = true
        return imageView
    }()
    
    private lazy var logoImgView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "alert_apply_logo")
        return imageView
    }()
    
    private lazy var closeBtn: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "alert_close"), for: .normal)
        return btn
    }()
    
    private lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.textColor = DBTextColor
        label.font = DBFuturaMaxiStd_DemiFont(14)
        label.numberOfLines = 0
        label.text = "title"
        label.textAlignment = .center
        return label
    }()
    
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        label.textColor = DBHexColor("#0A0716")
        label.font = DBFuturaMaxiStd_DemiFont(12)
        label.numberOfLines = 0
        label.text = "Tersedia Jalur VIP,Tingkat Acc Hingga 99%,Klik Untuk Mengajukan"
        label.textAlignment = .center
        return label
    }()
    
    private lazy var topBtn: UIButton = {
        let btn = UIButton()
        btn.clipsToBounds = true
        btn.backgroundColor = DBViewBgColor
        btn.titleLabel?.font = DBFuturaMaxiStd_DemiFont(12)
        btn.setTitle("top btn", for: .normal)
        btn.setTitleColor(DBViewBgColorSub, for: .normal)
        btn.layer.cornerRadius = 2
        return btn
    }()
    
    private lazy var bottomBtn: UIButton = {
        let btn = UIButton()
        btn.clipsToBounds = true
        btn.backgroundColor = DBViewBgColorSub
        btn.titleLabel?.font = DBFuturaMaxiStd_DemiFont(12)
        btn.setTitle("top btn", for: .normal)
        btn.setTitleColor(DBViewBgColor, for: .normal)
        btn.layer.cornerRadius = 2
        
        return btn
    }()
    
    private lazy var agreeBtn: UIButton = {
        let agreeBtn = UIButton()
        agreeBtn.setImage(UIImage(named: "login_agree_unselected"), for: .normal)
        agreeBtn.setImage(UIImage(named: "login_agree_selected"), for: .selected)
        //        agreeBtn.isSelected = true
        return agreeBtn
    }()
    
    private lazy var agreeLabel: YYLabel = {
        let agreeLabel = YYLabel()
        agreeLabel.numberOfLines = 0
        agreeLabel.preferredMaxLayoutWidth = DBScreenWidth - 90 - 67
        let agreeTipString = "saya telah membaca dan menyetujui hal di atas"
        let privacyString = ""
        let agreeString = agreeTipString + " " + privacyString
        let att = NSMutableAttributedString(string: agreeString)
        att.font = DBFuturaMaxiStd_BookFont(11)
        att.color = DBHexColor("#B1BEB5")
        agreeLabel.attributedText = att
        return agreeLabel
    }()
    
    private lazy var hLineView: UIView = {
        let view = UIView()
        view.backgroundColor = DBHexColor("#F6F6F6")
        view.isHidden = true
        return view
    }()
    
    private lazy var vLineView: UIView = {
        let view = UIView()
        view.backgroundColor = DBHexColor("#F6F6F6")
        view.isHidden = true
        return view
    }()
}

